package com.villaday.idf.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.villaday.idf.ActivityStackManager;
import com.villaday.idf.AppManager;
import com.villaday.idf.MainActivity;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 登录
 * */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.tv_content)
    public TextView tv_content;
    @BindView(R.id.checkbox)
    public CheckBox checkbox;


    private static final String App_ID = "wx2e033b1bbbeb6178";

    private IWXAPI api;

    boolean isExit;
    private Handler handler = new Handler();

    @Override
    protected int getViewResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {


        final SpannableStringBuilder style = new SpannableStringBuilder();

        //设置文字
        style.append("我已阅读并同意《爱订房服务协议》和《爱订房隐私政策》");

        //设置部分文字点击事件 《爱订房服务协议》
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {

                Intent intent = new Intent(LoginActivity.this, H5Activity.class);
                intent.putExtra("title", "");
                intent.putExtra("url", "https://res.villaday.com/static/pages/1907/19/190717-19.html");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                LoginActivity.this.startActivity(intent);
            }
        };
        style.setSpan(clickableSpan, 7, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置部分文字点击事件 《爱订房隐私政策》
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(LoginActivity.this, H5Activity.class);
                intent.putExtra("title", "");
                intent.putExtra("url", "https://res.villaday.com/static/pages/1907/19/190717-19.html");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                LoginActivity.this.startActivity(intent);
            }
        };
        style.setSpan(clickableSpan1, 17, 26, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_content.setText(style);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#46AFFF"));
        ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(Color.parseColor("#46AFFF"));

        style.setSpan(foregroundColorSpan, 7, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(foregroundColorSpan1, 17, 26, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        //配置给TextView
        tv_content.setMovementMethod(LinkMovementMethod.getInstance());
        tv_content.setText(style);
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @OnClick(R.id.ib_login)
    public void onViewClicked() {
        if (!checkbox.isChecked()) {
            ToastUtil.initToast(this, "请阅读并同意《爱订房服务协议》和《爱订房隐私政策》");
            return;
        }

        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, true);
        api.registerApp(Constant.ID.WeChat_APP_ID);
        if (!api.isWXAppInstalled()) {//未安装微信
            ToastUtil.initToast(this, "您未安装微信");
            return;
        }
        LoadingDialog loadingDialog = LoadingDialog.newInstance("跳转中");
        loadingDialog.show(getFragmentManager(), "loading");
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = Constant.ID.WeChat_State;
        api.sendReq(req);

    }


    @Override
    public void onBackPressed() {
        if (isExit) {
//            System.exit(0);
            AppManager.getAppManager().AppExit(this);
        } else {
            ToastUtil.initToast(this, "再按一次退出" + PhoneUtil.getAppName(this));
            isExit = true;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 5000);//5秒内再按后退键真正退出
        }
    }

}
